﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using Wjtour.Common;

namespace Wjtour.Ajax
{
    /// <summary>
    /// Summary description for login
    /// </summary>
    public class findpwd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            var mobile = context.Request["mobile"];
            var email = context.Request["email"];
            var code = context.Request["code"];

            Result r = new Result();
            if (string.IsNullOrWhiteSpace(mobile))
            {
                r.Errcode = "1";
                r.Errmsg = "请输入正确的手机号码！";
            }
            else if (string.IsNullOrWhiteSpace(email))
            {
                r.Errcode = "2";
                r.Errmsg = "请输入正确的邮箱地址！";
            }
            else if (string.IsNullOrWhiteSpace(code))
            {
                r.Errcode = "6";
                r.Errmsg = "请输入正确的验证码！";
            }
            else if (code.ToUpper() != context.Session["CheckCode"].ToString())
            {
                r.Errcode = "7";
                r.Errmsg = "验证码输入不正确！";
            }
            else
            {
                Wjtour.BLL.member_bll m_bll = new member_bll();
                Wjtour.Domain.member member = m_bll.CheckEmail(email.Trim());
                if (member == null)
                {
                    r.Errcode = "11";
                    r.Errmsg = "用户名与注册邮箱不符，请重新填写！";
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<html><head></head><body>");
                    sb.AppendLine("<p>你的账号是：" + member.email + "</p>");
                    sb.AppendLine("<p>你的密码是：" + member.pwd + "</p>");
                    sb.AppendLine("</body></html>");

                    Thread t = new Thread(new ThreadStart(() =>
                    {
                        try
                        {
                            Utility.SendMail(member.email, "吴江旅游网找回密码", sb.ToString());
                        }
                        catch (Exception) { }
                    }));
                    t.Start();
                    r.Errcode = "0";
                    r.Errmsg = "密码邮件已发送到您的邮箱！";
                }
            }

            JavaScriptSerializer serialzier = new JavaScriptSerializer();
            string output = serialzier.Serialize(r);
            context.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}